/*
 * FileName：PaperInfoCtrl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.ctrl;

import java.io.File;
import java.util.List;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.jpay.ext.kit.ZxingKit;
import com.smart.common.BaseCtrl;
import com.smart.common.util.AjaxResult;

import javax.annotation.Resource;

import com.smart.common.util.PropertiesUtil;
import com.smart.common.util.WxMemoryCacheClient;
import com.smart.wx.wxapi.process.MpAccount;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.smart.wx.wxcms.entity.PaperInfo;
import com.smart.wx.wxcms.service.PaperInfoService;

/**
 * 问卷调查基础表
 *
 * @author hermit
 * @version 2.0
 * @date 2018-07-23 21:22:41
 */
@Controller
@RequestMapping("/paperInfo")
public class PaperInfoCtrl extends BaseCtrl {
    @Resource
    private PaperInfoService paperInfoService;

    /**
     * 列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public AjaxResult list(PaperInfo paperInfo) throws Exception {
        String account = WxMemoryCacheClient.getMpAccount().getAccount();
        paperInfo.setAccount(account);
        List<PaperInfo> paperInfoList = paperInfoService.queryListByPage(paperInfo);
        return getResult(paperInfo, paperInfoList);
    }

    /**
     * 信息
     */
    @ResponseBody
    @RequestMapping("/info")
    public AjaxResult info(PaperInfo paperInfo) throws Exception {
        PaperInfo one = paperInfoService.queryObject(paperInfo);
        return AjaxResult.success(one);
    }

    /**
     * 保存
     */
    @ResponseBody
    @RequestMapping("/save")
    public AjaxResult save(PaperInfo paperInfo) throws Exception {
        String account = WxMemoryCacheClient.getMpAccount().getAccount();
        paperInfo.setAccount(account);

        return paperInfoService.saveOrUpdate(paperInfo);
    }


    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public AjaxResult update(PaperInfo paperInfo) throws Exception {
        paperInfoService.update(paperInfo);
        return AjaxResult.updateSuccess();
    }

    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping("/delete")
    public AjaxResult delete(PaperInfo paperInfo) throws Exception {
        paperInfoService.delete(paperInfo);
        return AjaxResult.deleteSuccess();
    }


    /**
     * 获取页面分页数据集合
     *
     * @param paperInfo
     * @return
     * @author sunday
     * @date 2018-04-02 17:40:56
     */
    @RequestMapping("/getDataTree")
    @ResponseBody
    public AjaxResult getDataTree(PaperInfo paperInfo)
            throws Exception {

        String account = WxMemoryCacheClient.getMpAccount().getAccount();
        paperInfo.setAccount(account);
        List<Object> list = paperInfoService.getDataTree(paperInfo);
        return AjaxResult.success(list);
    }

    /**
     * 查看链接
     */
    @ResponseBody
    @RequestMapping("/viewUrl")
    public AjaxResult viewUrl(PaperInfo paperInfo) throws Exception {
        PaperInfo one = paperInfoService.queryObject(paperInfo);

        /***** 生成二维码 **********/
        String account = WxMemoryCacheClient.getMpAccount().getAccount();
        String fileDir = PropertiesUtil.getString("res.upload.path");
        String resURL = PropertiesUtil.getString("res.upload.url");
        // 生成二维码
        String fileName = account + "_" + one.getId() + ".png";
        String filePath = fileDir + fileName;
        ZxingKit.encode(one.getPaperUrl(), BarcodeFormat.QR_CODE, 3, ErrorCorrectionLevel.H, "png", 200, 200, filePath);
        String paperUrl = resURL + fileName;
        one.setPaperQRCodeUrl(paperUrl);

        return AjaxResult.success(one);
    }


}
